package project.servlets;

import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Users.Client;
import schronisko.Animal;
import schronisko.Queries;

/**
 * Servlet implementation class Worker_functions
 */
@WebServlet("/Worker_functions")
public class Worker_functions extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	HttpSession s;
	Queries q = new Queries();
	Client c;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Worker_functions() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String function = (String)s.getAttribute("function");
		switch(function){
		case "edit_user":
			int step = (Integer)s.getAttribute("step");
			String username = (String)request.getParameter("username");
			if(step == 0) {
				c = q.getClient(username);
				s.setAttribute("edited_client", c);
				s.setAttribute("step", 1);
			}
			if(step == 1){
				c = (Client)s.getAttribute("edited_client");
				String name = (String)request.getParameter("name");
				String surname = (String)request.getParameter("surname");
				String post = (String)request.getParameter("post");
				String city = (String)request.getParameter("city");
				String house = (String)request.getParameter("house");
				String pesel = (String)request.getParameter("pesel");
				String id_card = (String)request.getParameter("id_card");
				
				if(!name.equals(""))
					c.setName(name);
				if(!surname.equals(""))
					c.setSurname(surname);
				if(!city.equals(""))
					c.setCity(city);
				if(!house.equals(""))
					c.setHouse(house);
				if(!post.equals(""))
					c.setPost(post);
				if(!pesel.equals(""))
					c.setPESEL(pesel);
				if(!id_card.equals(""))
					c.setID_card(id_card);
				q.updateClient(c);
				s.setAttribute("step", 2);
			}
			response.sendRedirect("worker/edit_user.jsp");
			break;
		case "edit_animal":
			int step_a = (Integer)s.getAttribute("step_a");
			String animal = request.getParameter("animal");
			if(step_a == 0) {
				int animals[] = q.getIDAnimal(animal);
				s.setAttribute("edit_anim", animals);
				s.setAttribute("step_a", 1);
			}
			if(step_a == 1){
				int anim_del = Integer.parseInt(request.getParameter("anim"));
				s.setAttribute("animal_edit", anim_del);
				ResultSet r = q.getAnimal(anim_del);
				s.setAttribute("editing_anim", r);
				s.setAttribute("step_a", 2);
			}
			if(step_a == 2){
				Animal a = new Animal("","","","","","","","");
				
				a.setID((Integer)s.getAttribute("animal_edit"));
				
				String name = request.getParameter("name");
				String gender = request.getParameter("gender");
				String race = request.getParameter("race");
				String date = request.getParameter("date");
				String access = request.getParameter("access");
				String inoc = request.getParameter("inoc");
				String desc = request.getParameter("desc");
				
				if(!name.equals(""))
					a.setName(name);
				if(gender != null)
					a.setGender(gender);
				if(!race.equals(""))
					a.setRace(race);
				if(!date.equals(""))
					a.setDate(date);
				if(access != null)
					a.setAccessibility(access);
				if(!inoc.equals(""))
					a.setLastInoculation(inoc);
				if(!desc.equals(""))
					a.setDescription(desc);

				if(q.updateAnimal(a) == 1)
				s.setAttribute("step_a", 3);
				else s.setAttribute("step_a", 4);
			}
			response.sendRedirect("worker/edit_animal.jsp");
			break;
		}
		
		
	}

}
